<?php
defined('PT_ACCESS') or die('No direct access');

class Language extends BaseControllers{
	private $default = LANGUAGE;
	private $directory;
	private $data = array();
	private $dbTable = 'language';
	private $langData = '';

	public function __construct($directory){
		$this->directory = $directory;
	}

	public function get($key){
		return (isset($this->data[$key]) ? $this->data[$key] : $key);
	}

	public function load($filename){
		$file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';

		if (file_exists($file)){
			$_ = array();

			require ($file);

			$this->data = array_merge($this->data, $_);

			return $this->data;
		}

		$file = DIR_LANGUAGE . $this->default . '/' . $filename . '.php';

		if (file_exists($file)){
			$_ = array();

			require ($file);

			$this->data = array_merge($this->data, $_);

			return $this->data;
		}else{
			trigger_error('Error: Could not load language ' . $filename . '!');
			exit();
		}
	}

	public function langList($admin = 0){

		global $db;

		$sql = '';
		if ($admin == 1)
			$sql .= ' AND lg_default = 0';

		$Qry = "SELECT lg_code, lg_name, lg_default
				FROM `{$this->dbTable}`
				WHERE lg_active = 'Y'" . $sql . "
				ORDER BY lg_id ASC";

		return $db->fetchAll($Qry);
	}

	public function total(){
		global $db;

		$Qry = "SELECT lg_code FROM `{$this->dbTable}` WHERE lg_active = 'Y'";

		return $db->numRows($Qry);
	}

	public function admin_lang($uid, $code = ''){

		global $codeLang;
		$lang	= '';
		$langs	= array();

		if (! $code)
			$lang = DEFAULT_LANG;
		else
			$lang = $code;

		if ($uid == 'root')
			$langs = $this->langData = $codeLang;
		else
			$langs = $this->langData = explode(',', $lang);

		return $langs;
	}
}
?>